﻿using System;
using IQIGame.Onigao.Logic;
using LFloatMath.Math;
using NetProtocol.POD;
using UnityEngine;

namespace IQIGame.Onigao.GamePlay
{
    public static class BattleUtil
    {
        public static Vector2POD ToPOD(this LVector2 pos)
        {
            return new Vector2POD()
            {
                x = pos.x.ToFloat(),
                y = pos.y.ToFloat(),
            };
        }

        public static LVector2 ToLV2(this Vector2POD pod)
        {
            if (pod == null)
            {
                return LVector2.zero;
            }
            return new LVector2(pod.x.ToLFloat(), pod.y.ToLFloat());
        }

        public static int GetValue(this FormulaConfig rConfig, IFormulaObject rSelf, IFormulaObject rTarget, int nDefault = 1)
        {
            return GetValue(rConfig, rSelf, rTarget, nDefault.ToLFloat()).ToInt();
        }

        public static LFloat GetValue(this FormulaConfig rConfig, IFormulaObject rSelf, IFormulaObject rTarget, LFloat nDefault, Skill rSkill = null)
        {
            if (rConfig == null)
            {
                return nDefault;
            }

            if (!rConfig.isFormula)
            {
                return rConfig.value.ToLFloat();
            }

            return Formula.Calculate<LFloat>(rConfig.value, rSelf, rTarget, rSkill);
        }

        public static ETargetRelation CheckRelation(Unit rSelf, Unit rTarget)
        {
            if (rSelf.teamIndex != rTarget.teamIndex)
            {
                return ETargetRelation.Enemy;
            }
            else if (rSelf == rTarget)
            {
                return ETargetRelation.Self;
            }
            else
            {
                return ETargetRelation.Friend;
            }
        }

        public static ETargetRelation CheckTeamRelation(int nSelfTeam, int nTargetTeam)
        {
            if (nSelfTeam != nTargetTeam)
            {
                return ETargetRelation.Enemy;
            }
            else
            {
                return ETargetRelation.Friend;
            }
        }

        public static T Get<T>(this T[] arr, int nIndex)
        {
            return arr?.Length > nIndex ? arr[nIndex] : default(T) ?? default(T);
        }
    }
}